package cn.icyzx.dao;

import org.apache.ibatis.jdbc.SQL;
import org.apache.log4j.Logger;
import cn.icyzx.util.WorksSelectCondition;
import cn.icyzx.util.StaticConstUtil;

import java.util.Map;


/**
 * @Author zk
 * @Date 2017/2/14.
 * @Email 2117251154@qq.com
 */

public class SqlProvider {

    private static Logger logger = Logger.getLogger(SqlProvider.class);

    public String getWorkByWid(final String wid) {
        final String sql = new SQL() {
            {
                SELECT("*");
                FROM("cyzx_work ");
                WHERE("wid = " + wid);
            }
        }.toString();

        logger.info(sql.replaceAll("\n", " "));
        return sql;

    }


    public String getUserByUid(final String uid) {
        String sql = new SQL() {{
            SELECT("*");
            FROM("user");
            WHERE("uid = " + uid);
        }}.toString();
        logger.info(sql.replaceAll("\n", " "));
        return sql;
    }


    public String packagingWorkConditionSql(Map<String, String> condition) {
        if (condition == null)
            return "";
        StringBuffer sql = new StringBuffer();
        final String key = condition.get(WorksSelectCondition.KEY);
        final String year = condition.get(WorksSelectCondition.YEAR);
        final String tag = condition.get(WorksSelectCondition.TAG);

        if (key != null) {
            sql.append(" WHERE name like '%" + key + "%'");
        } else if (year != null) {
            sql.append(" where publicTime >= '" + year + "'");
        } else if (tag != null) {
            sql.append(" where wid in (select wid from work_tag where tag = '" + tag + "') ");
        }


        String uid = condition.get(WorksSelectCondition.UID);
        String collectionUid = condition.get(WorksSelectCondition.COLLECTION_UID);
        String recommendUid = condition.get(WorksSelectCondition.RECOMMEND_UID);
        String commendUid = condition.get(WorksSelectCondition.COMMENT_UID);

        //getListWorksByUid
        if (null != uid)
            sql.append("where uid = " + uid);
        //getListWorksByCommentUid
        if (null != commendUid) {
            sql.append("where wid in ( select wid from comment where uid = " + commendUid + " )");
        }
        //getListWorksByRecommendUid
        if (null != recommendUid) {
            sql.append("where wid in ( select wid from work_store where type = 'recommend' and uid = " + recommendUid + " )");
        }
        //getListWorksByCollectionUid
        if (null != collectionUid) {
            sql.append("where wid in ( select wid from work_store where type = 'collection' and uid = " + collectionUid + " )");
        }
        return sql.toString();
    }

    public String getListWorks(Map<String, String> condition) {
        StringBuffer sql = new StringBuffer();
        sql.append(new SQL() {
            {
                SELECT("*");
                FROM("cyzx_work ");
            }
        });
        sql.append(packagingWorkConditionSql(condition));
        sql.append(" order by publicTime desc ");

        int pageSize = StaticConstUtil.PAGE_SIZE;
        int curPage = Integer.valueOf(condition.get("curPage") == null ? "1" : condition.get("curPage"));
        int begin = pageSize * (curPage - 1);
        sql.append(" limit " + begin + "," + pageSize);
        logger.info(sql.toString().replaceAll("\n", " "));
        return sql.toString();
    }

    public String getWorksSum(Map<String, String> condition) {
        StringBuffer sql = new StringBuffer("select count(*) from cyzx_work ");

        sql.append(packagingWorkConditionSql(condition));
        logger.info(sql.toString().replaceAll("\n", " "));
        return sql.toString();
    }

    public String getListCommentsByWid(final String wid) {
        StringBuffer sql = new StringBuffer();
        sql.append(new SQL() {
            {
                SELECT(" * ");
                FROM("cyzx_comment");
                WHERE("wid = " + wid);
            }
        });

        logger.info(sql.toString().replaceAll("\n", " "));
        return sql.toString();
    }

}
